package com.zjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.constants.SystemConstants;
import com.zjw.annotation.domain.ResponseResult;
import com.zjw.annotation.domain.entity.Comment;
import com.zjw.annotation.domain.vo.PageVo;
import com.zjw.annotation.domain.vo.RootCommentVo;
import com.zjw.annotation.domain.vo.SubCommentVo;
import com.zjw.mapper.CommentMapper;
import com.zjw.mapper.UserMapper;
import com.zjw.service.CommentService;
import com.zjw.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-12-14 18:46:39
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult commentList(Long articleId, Integer pageNum, Integer pageSize) {

//        查询表中id为文章id的根评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId, articleId);
        wrapper.eq(Comment::getType, SystemConstants.COMMENT_TYPE_ARTICLE);
        wrapper.eq(Comment::getRootId, SystemConstants.COMMENT_IS_ROOT);
//        分页
        IPage page = new Page(pageNum, pageSize);


//        查询出当前文章的所有根评论
        page(page, wrapper);
        List<Comment> rawRootList = page.getRecords();

//        拷贝到Vo中
        List<RootCommentVo> rootList = BeanCopyUtils.copyList(rawRootList, RootCommentVo.class);

        rootList.stream().forEach((root) -> {
//            先填写每个根评论的当前评论用户名
            root.setUsername(userMapper.selectById(root.getCreateBy()).getUserName());
//           传入根评论id返回子评论列表
            root.setChildren(getSubCommentList(root.getId(), root.getArticleId()));
        });

        return ResponseResult.okResult(new PageVo(rootList, page.getTotal()));
    }


    /**
     * 获取子评论列表
     *
     * @param rootId    根 ID
     * @param articleId 文章 ID
     * @return {@link List}<{@link SubCommentVo}>
     */
    private List<SubCommentVo> getSubCommentList(Long rootId, Long articleId) {
//        获取所以当前评论的子评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId, articleId);
        wrapper.eq(Comment::getType, SystemConstants.COMMENT_TYPE_ARTICLE);
        wrapper.eq(Comment::getRootId, rootId);
//        按创建时间升序
        wrapper.orderByAsc(Comment::getCreateTime);
//        获取指定根评论的所有子评论
        List<Comment> rawSubList = list(wrapper);
        List<SubCommentVo> subList = BeanCopyUtils.copyList(rawSubList, SubCommentVo.class);

        subList.forEach(e -> {
//            获取当前评论用户名
            e.setUsername(userMapper.selectById(e.getCreateBy()).getUserName());
//            获取当前评论回复名
//            查询回复评论的用户id
            e.setToCommentUserName(userMapper.selectById(getById(e.getToCommentId()).getCreateBy()).getUserName());
        });

        return subList;
    }


    /**
     * 保存评论
     *
     * @param comment 评论
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult saveComment(Comment comment) {
        save(comment);
        return ResponseResult.okResult();
    }

    /**
     * 友链评论列表
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult linkListCommentList(Integer pageNum, Integer pageSize) {

//        查询表中id为文章id的根评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getType, SystemConstants.COMMENT_TYPE_LINK);
        wrapper.eq(Comment::getRootId, SystemConstants.COMMENT_IS_ROOT);
        //        分页
        IPage page = new Page(pageNum, pageSize);


//        查询出当前文章的所有根评论
        page(page, wrapper);
        List<Comment> rawRootList = page.getRecords();

//        拷贝到Vo中
        List<RootCommentVo> rootList = BeanCopyUtils.copyList(rawRootList, RootCommentVo.class);

        rootList.stream().forEach((root) -> {
//            先填写每个根评论的当前评论用户名
            root.setUsername(userMapper.selectById(root.getCreateBy()).getUserName());
//           传入根评论id返回子评论列表
            root.setChildren(getLinkSubCommentList(root.getId()));
        });

        return ResponseResult.okResult(new PageVo(rootList, page.getTotal()));
    }


    /**
     * 获取链接子评论列表
     *
     * @param rootId    根 ID
     * @return {@link List}<{@link SubCommentVo}>
     */
    private List<SubCommentVo> getLinkSubCommentList(Long rootId) {
//        获取所以当前评论的子评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getType, SystemConstants.COMMENT_TYPE_LINK);
        wrapper.eq(Comment::getRootId, rootId);
//        按创建时间升序
        wrapper.orderByAsc(Comment::getCreateTime);
//        获取指定根评论的所有子评论
        List<Comment> rawSubList = list(wrapper);
        List<SubCommentVo> subList = BeanCopyUtils.copyList(rawSubList, SubCommentVo.class);

        subList.forEach(e -> {
//            获取当前评论用户名
            e.setUsername(userMapper.selectById(e.getCreateBy()).getUserName());
//            获取当前评论回复名
//            查询回复评论的用户id
            e.setToCommentUserName(userMapper.selectById(getById(e.getToCommentId()).getCreateBy()).getUserName());
        });

        return subList;
    }

}

